package com.wardrobe.soa.backend.shop;

import com.google.common.collect.Maps;
import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.domain.shop.ShopFromBean;
import com.wardrobe.domain.shop.ShopShowBean;
import com.wardrobe.domain.train_manage.TrainManageShowBean;
import com.wardrobe.soa.api.user.UserDao;
import com.wardrobe.soa.backend.train_manage.BackTrainManageBean;
import com.wardrobe.soa.backend.train_manage.BackTrainManageController;
import com.wardrobe.soa.backend.train_manage.BackTrainManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/16.
 */
@RestController
@RequestMapping("/backend/shop")
public class BackShopController {
    private static final Logger logger = LoggerFactory.getLogger(BackShopController.class);
    @Autowired
    private BackShopService backShopService;
    @Autowired
    private UserDao userDao;
    @RequestMapping("/shopList")
    public Map<String, Object> getshopList(ShopFromBean shopFromBean) {
        Map<String, Object> result = Maps.newHashMap();
        //商家的
        shopFromBean.setType(0);
        List<ShopShowBean> list = backShopService.getShopList(shopFromBean);
        list.stream().forEach(showBean -> {
            showBean.setCreateTime(showBean.getCreateTime().substring(0,showBean.getCreateTime().length()-2));
            showBean.setUpdateTime(showBean.getUpdateTime().substring(0,showBean.getUpdateTime().length()-2));
            Map<String, Object> userInfoById = userDao.getUserInfoById(showBean.getUserId());
            showBean.setMobile((String) userInfoById.get("mobile_number"));
        });
        long count = backShopService.countShop(shopFromBean);
        int pageCount = (int) Math.ceil(((double) count / shopFromBean.getPageSize()));
        result.put("list", list);
        result.put("count", count);
        result.put("pageCount", pageCount);
        result.put("pageSize", shopFromBean.getPageSize());
        return result;
    }
    @RequestMapping("/systemShopList")
    public Map<String, Object> systemShopList(ShopFromBean shopFromBean) {
        Map<String, Object> result = Maps.newHashMap();
        //商家的
        shopFromBean.setStatus(-2);
        shopFromBean.setType(1);
        List<ShopShowBean> list = backShopService.getShopList(shopFromBean);
        list.stream().forEach(showBean -> {
            showBean.setCreateTime(showBean.getCreateTime().substring(0,showBean.getCreateTime().length()-2));
            showBean.setUpdateTime(showBean.getUpdateTime().substring(0,showBean.getUpdateTime().length()-2));
        });
        long count = backShopService.countShop(shopFromBean);
        int pageCount = (int) Math.ceil(((double) count / shopFromBean.getPageSize()));
        result.put("list", list);
        result.put("count", count);
        result.put("pageCount", pageCount);
        result.put("pageSize", shopFromBean.getPageSize());
        return result;
    }
    @RequestMapping("/modifyShop")
    public Map<String,Object> modifyShop(BackEndFromBean fromBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            fromBean.setUpdateTime(new Date());
            int status =backShopService.modifyShop(fromBean);
            if(status==1){
                result.put("status",status);
                result.put("msg","修改成功");
            }else{
                result.put("status",2);
                result.put("msg","修改失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("msg","修改失败");
            logger.error("modifyShop error",e);
        }
        return result;
    }

    @RequestMapping("/getShop")
    public Map<String,Object> getShop(String nickName){
        ShopShowBean shop = backShopService.getShop(nickName);
        Map<String,Object> map = Maps.newHashMap();
        if(shop!=null){
            map.put("status",1);
            map.put("shopId",shop.getId());
        }
        return map;
    }

    @RequestMapping("/getOneShopDetail")
    public Map<String, Object> getOneShopDetail(long shopId) {
        Map<String, Object> result = Maps.newHashMap();
        //商家的
        List<ShopShowBean> list = new ArrayList<>();
        ShopShowBean oneShopDetail = backShopService.getOneShopDetail(shopId);
        list.add(oneShopDetail);
        list.stream().forEach(showBean -> {
            showBean.setCreateTime(showBean.getCreateTime().substring(0,showBean.getCreateTime().length()-2));
            showBean.setUpdateTime(showBean.getUpdateTime().substring(0,showBean.getUpdateTime().length()-2));
        });
        result.put("list", list);
        return result;
    }
}
